package com.xgd.phylw.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xgd.phylw.bean.Hall;
import com.xgd.phylw.bean.Movie;
import com.xgd.phylw.bean.Movietemp;
import com.xgd.phylw.dto.Movie_tempDto;
import com.xgd.phylw.service.HallService;
import com.xgd.phylw.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
public class MovieController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private HallService hallService;

    @RequestMapping(value = "/ShowMovies",method = RequestMethod.POST)
    @ResponseBody
    public List<Movie_tempDto> ShowMovie(Movie movie){
        List<Movie_tempDto> movieList = movieService.SelectAllMovie(movie);
        for (Movie_tempDto m:movieList){
            System.out.println(m);
        }
        return movieList;
    }

    @RequestMapping(value = "/ShowMovies2")
    public String ShowMovie2(Movie movie,Model model){
        List<Movie_tempDto> movieList = movieService.SelectAllMovie(movie);
        List<Hall> halls = hallService.SelectAllHall();
        model.addAttribute("halls",halls);
        model.addAttribute("movieTemp",movie.getMovieTemp());
        model.addAttribute("movieList",movieList);
        return "cinema";
    }
    //后台影片模糊查询 条件（主演，名字）
    @RequestMapping("searchMovie")
    public String PageChangeToSearchUser(String movie,Model model
            ,@RequestParam(value = "pageSize",required = false,defaultValue = "5") int pageSize
            ,@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum){

        Page<Object> objectPage = PageHelper.startPage(pageNum,pageSize);
        List<Movie> movies = movieService.MovieAll(movie);

        model.addAttribute("pages",objectPage.toPageInfo());
        model.addAttribute("movies",movies);
        return "admin_movie";
    }
    //后台影片 类型 模糊查询 条件（类型，名字）
    @RequestMapping("searchMovieType")
    public String searchMovieType(String movie,Model model
            ,@RequestParam(value = "pageSize",required = false,defaultValue = "12") int pageSize
            ,@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum){

        Page<Object> objectPage = PageHelper.startPage(pageNum,pageSize);
        List<Movie> movies = movieService.MovieTempAll(movie);

        model.addAttribute("pages",objectPage.toPageInfo());
        model.addAttribute("movieTemp",movies);
        model.addAttribute("pageNull",movie);
        return "movie_type";
    }
    //后台影片详情
    @RequestMapping("showMovie")
    public String showMovie(String movie,Model model){
        Movie movie1 = new Movie();
        movie1.setMovieName(movie);
        Movie movies = movieService.MovieByName(movie1);
        model.addAttribute("movie_ck",movies);
        return "movie_update";
    }
    //后台影片删除
    @RequestMapping("MovieDelect")
    public String MovieDelete(String[] movieName,String type){
        System.out.println("MovieDelect:\t"+type);

        Movie movie = new Movie();
        for (String s: movieName){
            movie.setMovieName(s);
            Movie byNameId = movieService.MovieByName(movie);
            movieService.movieTepDelete(byNameId.getMovieId());
        }
        movie.setMovieTime(0);
        int j = movieService.movieTempDel(movie);
//        int i = movieService.movieDelete(movieName);

        if (type !=null){
            if (j==1){
                return "redirect:MovieType";
            }
            return "redirect:MovieType";
        }else {
            if (j==1){
                return "redirect:AdminMovie";
            }
            return "redirect:AdminMovie";
        }
    }
    //后台影片添加
    @RequestMapping("MovieAllUpdate")
    public String MovieAdd(Movie movie, MultipartFile img, HttpSession session) throws IOException {
        movie.setMovieTime(0);
        String realPath =  session.getServletContext().getRealPath("");
        realPath +="/images/movieimages/";
        String headPath = "images/movieimages/"+img.getOriginalFilename();
        File file = new File(realPath + img.getOriginalFilename());
        movie.setMovieImg(headPath);
        if (movieService.movieTempAdd(movie)==1) {
            img.transferTo(file);
            return "redirect:AdminMovie";
        }
        return "movie_add";
    }

    //后台影片 类型 添加
    @RequestMapping("MovieTypeUpdate")
    public String MovieTypeUpdate(Movietemp movietemp,int time){
        System.out.println(movietemp);
        if(time != 0){
            Movie movie = new Movie();
            movie.setMovieTime(time);
            movie.setMovieId(movietemp.getMovieId());
            int j = movieService.movieTempById(movie);
        }

        int i = movieService.movieTempInsert(movietemp);
        if (i==1) {
            return "redirect:MovieType";
        }
        return "type_add";
    }
    //后台影片 类型 添加
    @RequestMapping("MovieTypeUpdate2")
    public String MovieTypeUpdate2(Movietemp movietemp){
        int i = movieService.movieTempInsert(movietemp);
        if (i==1) {
            return "redirect:MovieType";
        }
        return "type_add";
    }
    @RequestMapping("TypeUpdate")
    public String PageChangeToTypeUpdate(String movie,Model model){
        Movie movie1 = new Movie();
        movie1.setMovieName(movie);
        Movie movies = movieService.MovieByName(movie1);
        model.addAttribute("movie", movies);
        return "type_update";
    }
}
